var game_log_dao = require("../dao/game_log_dao");

var yuxiaxie_api = {};

yuxiaxie_api.getYXXGameRecord = function(socket, data) {
    var limit = 10;
    var user_id = socket.userId;
    var now_page = data.now_page;
    
    game_log_dao.getYXXGameRecord({limit: limit, user_id: user_id, now_page: now_page}, function (state, rows) {
        var result_data = [];
        if (!rows) {
            socket.emit('getYXXGameRecordResult', {
                result: true,
                data: result_data,
            });
            return;
        }
        for (var x in rows) {
            var table_dict = JSON.parse(rows[x].table_dict);
            var add_date = rows[x].add_date;
    
            var all_change_user_list = {};
            for (var i in table_dict.game_win_record) {
                var change_user_list = table_dict.game_win_record[i].change_user_list;
                for (var v in change_user_list) {
                    if (!all_change_user_list[v]) {
                        all_change_user_list[v] = 0
                    }
                    if (!change_user_list[v]) {
                        change_user_list[v] = 0
                    }
                    all_change_user_list[v] += change_user_list[v]
                }
    
            }
            var res_user_list = {};
            if (table_dict.game_win_record[0]) {
                res_user_list = table_dict.game_win_record[table_dict.game_win_record.length - 1].res_user_list
            }
    
            result_data.push({
                ju_shu: table_dict.ju_shu,                                 //局数
                fang_ka_shu: table_dict.fang_ka_shu,                      //房卡数
                user_name_dict: table_dict.user_name_dict,            //桌子内玩家对应名字
                user_head_url_dict: table_dict.user_head_url_dict,            //对应头像
                game_type: table_dict.game_type,                      //游戏类型
                tableKey: table_dict.tableKey,                       //房间号
                game_win_record: table_dict.game_win_record,      //用户游戏记录
                all_change_user_list: all_change_user_list,      //用户总金币变动
                res_user_list: res_user_list,   //用户最后一局数
                add_date: add_date,                                //记录创建时间
                is_qiang_zhuang: table_dict.is_qiang_zhuang,                                //是否抢庄
                zhuang: table_dict.zhuang,                                //庄
            });
        }
        socket.emit('getYXXGameRecordResult', {
            result: true,
            data: result_data,
        });
    }); 
} 

module.exports = yuxiaxie_api;